package com.juhuacha.tryrxjava;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.juhuacha.tryrxjava.widget.CollapsibleTextView;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewDemoAct extends AppCompatActivity {

    List<BaseBean> mDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view_demo);

        RecyclerView view = (RecyclerView) findViewById(R.id.recyclerView);

        mDatas = new ArrayList<>();

        for (int i = 0; i < 20; i++) {
            mDatas.add(new BaseBean(i+"叫撒打算斗奥斯个偶滴哦亲很无敌哦群殴我掉请教我的机器的收费费全额付潜伏期为皮肤好看的舒服哈市的更换皮肤是不过标普根本我个欧虎频繁贵啊不得扫电话覅是的覅偶【哈市地方hi阿斯顿和覅哈市迪欧放还是动画片覅合适的爱上当减肥【片身上的机票是大家发技术都【佛【啊速度金佛【加上东风牌是电话覅哦啊啥活动频繁敷偶的话十多个覅按时的高发", false,true));
        }

        LinearLayoutManager manager = new LinearLayoutManager(RecyclerViewDemoAct.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        view.setLayoutManager(manager);
        view.setAdapter(new RecyclerAdapter());

    }

    class RecyclerAdapter extends RecyclerView.Adapter<ViewHolder>{


        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            ViewHolder holder = new ViewHolder(LayoutInflater.from(RecyclerViewDemoAct.this).inflate(R.layout.item_tests,null,false));

            return holder;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {

            final int i = position;

            holder.text.setmCallback(new CollapsibleTextView.CollapsStateCallBack() {
                @Override
                public void stateSpread() {
                    mDatas.get(i).setExpand(true);
                    mDatas.get(i).setFirst(false);
                }

                @Override
                public void stateShrinkUp() {
                    mDatas.get(i).setExpand(false);
                    mDatas.get(i).setFirst(false);
                }

                @Override
                public void stateNone() {
                    mDatas.get(i).setFirst(false);
                }
            });

            if ( mDatas.get(position).isFirst()) {

                //holder.text.setFlag(true);
                holder.text.setDesc(mDatas.get(position).getContent(), TextView.BufferType.NORMAL);


                // holder.text.setFlag(true);
            } else {

                if (mDatas.get(position).isExpand()) {
                    //text.setmState(CollapsibleTextView.COLLAPSIBLE_STATE_SPREAD);
                    holder.text.setDesc(mDatas.get(position).getContent(), TextView.BufferType.NORMAL, CollapsibleTextView.COLLAPSIBLE_STATE_SPREAD);
                } else {
                    //text.setmState(CollapsibleTextView.COLLAPSIBLE_STATE_SHRINKUP);
                    holder.text.setDesc(mDatas.get(position).getContent(), TextView.BufferType.NORMAL, CollapsibleTextView.COLLAPSIBLE_STATE_SHRINKUP);
                }


            }
        }

        @Override
        public int getItemCount() {
            return mDatas != null ? mDatas.size() : 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        CollapsibleTextView text;

        public ViewHolder(View itemView) {
            super(itemView);
            text = (CollapsibleTextView) itemView.findViewById(R.id.collaps);
        }

    }

    class BaseBean {
        private String content;
        private boolean isExpand;
        private boolean isFirst;

        public BaseBean() {
        }

        public BaseBean(String content, boolean isExpand,boolean isFirst) {
            this.content = content;
            this.isExpand = isExpand;
            this.isFirst = isFirst;
        }

        public boolean isFirst() {
            return isFirst;
        }

        public void setFirst(boolean first) {
            isFirst = first;
        }

        public boolean isExpand() {
            return isExpand;
        }

        public void setExpand(boolean expand) {
            isExpand = expand;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }
    }
}
